package com.czxy.service;

import com.alibaba.fastjson.JSON;
import com.czxy.config.JwtProperties;
import com.czxy.util.JwtUtils;
import com.czxy.vo.CartRequest;
import com.czxy.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 张青卯
 */
@Service
public class CartService {

    private final JwtProperties jwtProperties;

    private final RedisTemplate<Integer,String> redisTemplate;

    @Autowired
    public CartService(JwtProperties jwtProperties, RedisTemplate<Integer, String> redisTemplate) {
        this.jwtProperties = jwtProperties;
        this.redisTemplate = redisTemplate;
    }

    public void addToCart(CartRequest cartRequest, HttpServletRequest request) {
        String token = request.getHeader("authorization");
        UserInfo userInfo = null;
        boolean flag = false;
        try {
            userInfo = JwtUtils.getInfoFromToken(token, jwtProperties.getPublickey());
        } catch (Exception e) {
            flag = true;
            e.printStackTrace();
        }
        if (flag){
            return;
        }
        Integer id = userInfo.getId();
        String carts = redisTemplate.opsForValue().get(id);
        if (carts == null){
            ArrayList<CartRequest> list = new ArrayList<>();
            list.add(cartRequest);
            redisTemplate.opsForValue().set(id,JSON.toJSONString(list));
        }else {
            List<CartRequest> list = JSON.parseArray(carts, CartRequest.class);
            for (CartRequest user : list) {
                if (user.getSkuid().equals(cartRequest.getSkuid())){
                    user.setCount(user.getCount()+cartRequest.getCount());
                }else {
                   list.add(cartRequest);
                }
            }
            redisTemplate.opsForValue().set(id, JSON.toJSONString(list));
        }
    }

    public List<CartRequest> getShoppingCart(HttpServletRequest request) {
        UserInfo info = getUserInfo(request);
        assert info != null;
        String cart = redisTemplate.opsForValue().get(info.getId());
        return JSON.parseArray(cart, CartRequest.class);
    }

    private UserInfo getUserInfo(HttpServletRequest request){
        String token = request.getHeader("authorization");
        try {
            return JwtUtils.getInfoFromToken(token, jwtProperties.getPublickey());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void modifyShoppingCart(Integer skuid, CartRequest cartRequest, HttpServletRequest request) {
        List<CartRequest> list = getShoppingCart(request);
        assert list != null;
        for (CartRequest cartRequest1 : list) {
            if (cartRequest1.getSkuid().equals(skuid)){
                cartRequest1.setChecked(cartRequest.getChecked());
                cartRequest1.setCount(cartRequest.getCount());
                break;
            }
        }
    }

    public void deleteShoppingCart(Integer id, HttpServletRequest request) {
        UserInfo info = getUserInfo(request);
        assert info != null;
        String cart = redisTemplate.opsForValue().get(info.getId());
        List<CartRequest> list = JSON.parseArray(cart, CartRequest.class);
        assert list != null;
        for (CartRequest cartRequest : list) {
            if (cartRequest.getSkuid().equals(id)){
                list.remove(cartRequest);
                break;
            }
        }
        redisTemplate.opsForValue().set(info.getId(),JSON.toJSONString(list));
    }

    public void addLoginCart(Integer userId, String carts) {
        List<CartRequest> cartRequests = JSON.parseArray(carts, CartRequest.class);
        String s = redisTemplate.opsForValue().get(userId);
        if (s == null){
            redisTemplate.opsForValue().set(userId,JSON.toJSONString(cartRequests));
        }else {
            List<CartRequest> reidsRequests = JSON.parseArray(s, CartRequest.class);
            for (CartRequest c1 : cartRequests) {
                CartRequest cart = null;
                for (CartRequest c2 : reidsRequests) {
                    if (c1.getSkuid().equals(c2.getSkuid())){
                        cart = c2;
                    }
                }
                if (cart != null){
                    cart.setCount(cart.getCount()+c1.getCount());
                }else {
                    reidsRequests.add(c1);
                }
            }
            redisTemplate.opsForValue().set(userId,JSON.toJSONString(reidsRequests));
        }
    }
}